/*
 * Profile Controller
 * @Author: chiangbt@geoportal.cn 
 * @Date: 2019-05-15 15:04:36 
 * @Last Modified by: chiangbt@geoportal.cn
 * @Last Modified time: 2019-05-15 15:17:40
 */
const mongoose = require('mongoose')
const Profile = require("../models/profile");
const User = require("../models/user");

exports.create_profile = (req, res, next) => {
    User.findById(req.body.user)
        .then(user => {
            if (!user) {
                return res.status(404).json({
                    success: false,
                    message: "User not found"
                });
            }
            const profile = new Profile({
                _id: mongoose.Types.ObjectId(),
                user: req.body.user,
                phone: req.body.phone,
                address: req.body.address
            });
            profile.save()
                .then(result => {
                    return res.status(201).json({
                        success: true,
                        message: "Profile创建成功",
                        order: {
                            id: result._id,
                            user: result.user,
                            phone: result.phone,
                            address: result.address
                        }
                    });
                })
                .catch(err => {
                    return res.status(500).json({
                        success: false,
                        error: err.message
                    });
                });
        })
};

exports.profile_get_by_id = (req, res, next) => {
    const id = req.params.id;

    Profile.findById(id)
        .select("_id user phone address")
        .populate("phone", "address")
        .then(profile => {
            if (!profile) {
                return res.status(404).json({
                    success: false,
                    message: "Profile not found"
                });
            } else {
                return res.status(200).json({
                    success: true,
                    profile: {
                        id: profile._id,
                        phone: profile.phone,
                        address: profile.address,
                        user: {
                            id: profile.user._id,
                            name: profile.user.name
                        }
                    }
                });
            }
        })
        .catch(err => {
            return res.status(500).json({
                success: false,
                message: err.message
            });
        });
};

exports.update_profile = (req, res, next) => {
    const id = req.params.id;
    const profile = req.body;
    const {
        ...updateData
    } = profile

    Profile.findByIdAndUpdate({
            _id: id
        }, updateData, {
            new: true
        })
        .select('_id address phone user createdAt')
        .then(profile => {
            if (profile) {
                return res.status(200).json({
                    success: true,
                    message: '更新 profile 对象成功.',
                    profile: {
                        id: profile._id,
                        phone: profile.phone,
                        address: profile.address,
                        user: {
                            id: profile.user._id,
                            name: profile.user.name
                        }
                    }
                });
            } else {
                return res.status(400).json({
                    success: false,
                    error: '更新的profile并不存在'
                });
            }
        })
        .catch(err => {
            return res.status(500).json({
                success: false,
                error: err.message
            });
        });
}